<?php
/**
 * Created by PhpStorm.
 * User: Lenovo
 * Date: 2019/4/25
 * Time: 20:01
 */

namespace app\admin\model;
use think\Db;
use think\Session;
class Chongzhi extends BaseModel
{
    protected $table='jc_chongzhi';

    public function chongzhiindex()
    {
               //获取当前登陆的id
        $uid = Session::get('admin_uid');
        $vip = Guanli::where('id',$uid)->find();
        $arr = User::where('uid',$vip['uid'])->find();
        $rs=$this
            ->alias('cz')
            ->join('jc_user us','cz.uid=us.uid','left')
            ->join('jc_zpaytype zp','zp.zid=cz.zid','left')
            ->order('cz.c_createtime desc')
            ->paginate(5);
        if($arr['daili']==1)
        {
          $rs = $this
               ->alias('cz')
               ->join('jc_user us','cz.uid=us.uid','left')
               ->join('jc_zpaytype zp','zp.zid=cz.zid','left')
               ->where('us.u_path','like','%,'.$arr['uid'].',%')
               ->paginate(5);
          if(input('sub'))
          {
              $cateid=input('cateid','');//下拉菜单
              $key=input('Key');//输入框
              $datemin = input('datemin', '');//最小时间
              $datemax = input('datemax', '');//最大的吧
              //判断$key是不是空
              //判断时间
              if($key=='充值完成')
              {
                  $key=3;
              }
              elseif ($key=='已退回')
              {
                  $key=2;
              }
              elseif ($key=='等待充值')
              {
                  $key=1;
              }
              if(isset($datemin) && $datemin != ''){
                  $rs = $rs->whereTime('jc_chongzhi.c_createtime', '>=', input('datemin').' 00:00:00');
                  //print_r($rs);die;
              }
              if(isset($datemax) && $datemax != ''){
                  $rs = $rs->whereTime('jc_chongzhi.c_createtime', '<=', input('datemax').' 23:59:59');
              }
              if($cateid=='')
              {
                  return $rs;
              }
              if($key=='')
              {
                  return $rs;
              }
             
            	$rs = $this
               ->alias('cz')
               ->join('jc_user us','cz.uid=us.uid','left')
               ->join('jc_zpaytype zp','zp.zid=cz.zid','left')
               ->where('us.u_path','like','%,'.$arr['uid'].',%')
               ->where($cateid,'like','%'.$key.'%')
               ->paginate(5);
              return $rs;
          }
          return $rs;
        }else{
          if(input('sub'))
          {
              $cateid=input('cateid','');//下拉菜单
              $key=input('Key');//输入框
              if($cateid=='')
              {
                  return $rs;
              }
              if($key=='')
              {
                  return $rs;
              }
              $rs=$this
            ->alias('cz')
            ->join('jc_user us','cz.uid=us.uid','left')
            ->join('jc_zpaytype zp','zp.zid=cz.zid','left')
            ->order('cz.c_createtime desc')
            ->where($cateid,'like','%'.$key.'%')->paginate(4);
              return $rs;
          }
          else
          {
              return $rs;
          }
        }
    }
    //充值拒绝原因
    public function chongzhireason()
    {
        $data=input('post.');
        $rs=$this->where('cid',$data['cid'])->update(['c_state'=>2,'c_reason'=>$data['c_reason']]);
        if ($rs!==false)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
    //充值到账确定
    public function chongzhi()
    {
        Db::startTrans();
        $cid=input('cid');
        $rs=$this->alias('cz')->join('jc_user us','us.uid=cz.uid','left')->where('cid',$cid)->find();
        //print_r($rs);die;
        $cha=User::where('uid',$rs['uid'])->find();
        $ar=array($rs['c_money'],$cha['u_money']);//信用余额和充值额度放进数组
        $money=array_sum($ar);//求和函数进行求和
        $xiu=User::where('uid',$rs['uid'])->update(['u_money'=>$money]);//充值信用金额到账
        $state=$this->where('cid',$cid)->update(['c_state'=>3]);//充值状态发生改变
        if ($xiu && $state)
        {
            Db::commit();//提交事务
            return 1;
        }
        else
        {
            Db::rollback();// 回滚事务
            return 0;
        }
    }
}